package com.gelapps.memogel.accounts;

import com.gelapps.memogel.Constants;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class AccountXMLHandler extends DefaultHandler{
	
	 Boolean currentElement = false;
	    String currentValue = "";
	    AccountsMaster item = null;
	    private ArrayList<AccountsMaster> itemsList = new ArrayList<AccountsMaster>();
	 
	    public ArrayList<AccountsMaster> getItemsList() {
	        return itemsList;
	    }
	 
	    // Called when tag starts 
	     @Override
	    public void startElement(String uri, String localName, String qName,
	            Attributes attributes) throws SAXException {
	 
	        currentElement = true;
	        currentValue = "";
	        if (localName.equals(Constants.getAccountElement())) {
	            item = new AccountsMaster();
	        } 
	 
	    }
	 
	    // Called when tag closing 
	    @Override
	     public void endElement(String uri, String localName, String qName)
	    		    throws SAXException {
	 
	        currentElement = false;
	 
	        /** set value */
	        if (localName.equalsIgnoreCase(Constants.getAccountName()))                   item.setName(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountServer()))            item.setServer(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountPath()))              item.setPath(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountUsername()))          item.setUsername(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountPassword()))          item.setPassword(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountProvider()))          item.setProvider(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountProviderWebSite()))   item.setProviderWebSite(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountProviderPhone()))     item.setProviderPhone(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountNote()))              item.setNote(currentValue);
	        else if (localName.equalsIgnoreCase(Constants.getAccountElement()))           itemsList.add(item);
	    }
	 
	    // Called to get tag characters 
	    @Override
	    public void characters(char[] ch, int start, int length)
	    throws SAXException {
	 
	        if (currentElement) {
	            currentValue = currentValue +  new String(ch, start, length);
	        }
	 
	    }
}
